package com.lin.server.impl;

import com.lin.dataobject.Pharmacy;
import com.lin.repository.PharmacyRepository;
import com.lin.server.PharmacyService;
import com.lin.utils.DateUtils;
import com.lin.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by MYQ on 2017/10/28.
 */
@Service
public class PharmacyServiceImpl implements PharmacyService{
    @Autowired
    private PharmacyRepository repository;


    private Specification<Pharmacy> getSpecification(String name,String startTime, String endTime){
        return new Specification<Pharmacy>() {
            @Override
            public Predicate toPredicate(Root<Pharmacy> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if (StringUtils.isNotEmpty(name)) {
                    predicates.add(criteriaBuilder.like(root.get("name"), "%" + name + "%"));
                }
                if (StringUtils.isNotEmpty(startTime)){
                    Date start= DateUtils.stringToDate(startTime);
                    predicates.add(criteriaBuilder.greaterThan(root.get("createTime"),start));
                }
                if (StringUtils.isNotEmpty(endTime)){
                    Date end=DateUtils.addDay(endTime,1);
                    predicates.add(criteriaBuilder.lessThan(root.get("createTime"),end));
                }
                return criteriaQuery.where(predicates.toArray(
                        new Predicate[predicates.size()])).getRestriction();
            }
        };
    }

    @Override
    public Pharmacy findOne(String id) {
        return repository.findOne(id);
    }

    @Override
    public List<Pharmacy> findAll(String name, String startTime, String endTime) {
        return repository.findAll(getSpecification(name,startTime,endTime));

    }

    @Override
    public Page<Pharmacy> findAll(String name, String startTime, String endTime, Pageable pageable) {
        return repository.findAll(getSpecification(name,startTime,endTime),pageable);
    }

    @Override
    public Pharmacy save(Pharmacy pharmacy) {
        return repository.save(pharmacy);
    }

    @Override
    public void del(Pharmacy pharmacy) {
        repository.delete(pharmacy);
    }

    @Override
    public Long getSize(String name, String startTime, String endTime) {
        return repository.count(getSpecification(name,startTime,endTime));
    }

    @Override
    public List<Pharmacy> findAll() {
        return repository.findAll();
    }
}
